﻿using System.Collections.Generic;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_Between_Enumerables
    {
        private readonly FromPerson fromPerson;
        private readonly Mapping<FromPerson, ToPerson> mapping;
        private ToPerson toPerson;

        public class FromPerson
        {
            public List<int> List { get; set; }
            public IList<int> IList { get; set; }
            public ICollection<int> ICollection { get; set; }
            public IEnumerable<int> IEnumerable { get; set; }
        }

        public class ToPerson
        {
            public List<int> List { get; set; }
            public IList<int> IList { get; set; }
            public ICollection<int> ICollection { get; set; }
            public IEnumerable<int> IEnumerable { get; set; }
        }

        public When_mapping_Between_Enumerables()
        {
            mapping = new Mapping<FromPerson, ToPerson>();
            mapping.AutoRelateEqualNames();

            fromPerson = new FromPerson
                 {
                     List = new List<int> {1, 2, 3, 4},
                     IList = new List<int> {1, 2, 3, 4},
                     ICollection = new List<int> {1, 2, 3, 4},
                     IEnumerable = new List<int> {1, 2, 3, 4}
                 };

            toPerson = mapping.Map(fromPerson, new ToPerson());
        }

        [Fact]
        public void Should_map_between_Lists()
        {
            AssertEqual(fromPerson.List, toPerson.List);
        }

        [Fact]
        public void Should_map_between_ICollections()
        {
            AssertEqual(fromPerson.ICollection, toPerson.ICollection);
        }
        [Fact]
        public void Should_map_between_IEnumerables()
        {
            AssertEqual(fromPerson.IEnumerable, toPerson.IEnumerable);
        }

        [Fact]
        public void Should_map_between_ILists()
        {
            AssertEqual(fromPerson.IList, toPerson.IList);
        }

        public static void AssertEqual(IEnumerable<int> from, IEnumerable<int> to)
        {
            var toEnumerator = to.GetEnumerator();
            foreach (var fromElement in from)
            {
                Assert.True(toEnumerator.MoveNext());
                Assert.Equal(fromElement,toEnumerator.Current);
            }
            Assert.False(toEnumerator.MoveNext());
        }
    }
}
